package com.cby.android.olive.utils

import android.view.View
import android.view.ViewGroup
import java.util.*
import java.util.ArrayDeque

/**
 * Desc：
 * author：Christiano
 */
object ViewUtil {
    /**
     * 获取指定类型的子View
     *
     * @param group viewGroup
     * @param cls   如：RecyclerView.class
     * @param <T>
     * @return 指定类型的View
    </T> */
    fun <T> findTypeView(group: ViewGroup?, cls: Class<T>): T? {
        if (group == null) {
            return null
        }
        //双向队列
        val deque: Deque<View> = ArrayDeque()
        deque.add(group)
        while (!deque.isEmpty()) {
            //取第一个元素
            val node = deque.removeFirst()
            if (cls.isInstance(node)) {
                return cls.cast(node)
            } else if (node is ViewGroup) {
                val container = node
                var i = 0
                val count = container.childCount
                while (i < count) {

                    //添加到队尾
                    deque.add(container.getChildAt(i))
                    i++
                }
            }
        }
        return null
    }
}